<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>div水平垂直居中示例</title>
    <style>
        /* 通用样式 */
        .demo-container {
            margin: 20px 0;
            border: 1px solid #ccc;
            height: 200px;
            position: relative;
        }
        .demo-title {
            padding: 5px;
            background-color: #f0f0f0;
            font-weight: bold;
        }
        .center-box {
            background-color: #3498db;
            color: white;
            text-align: center;
            width: 100px;
            height: 100px;
        }
        /* 方法2: Grid */
        .method2 {
            display: grid;
            place-items: center;
        }
        
        /* 方法3: 绝对定位 + transform */
        .method3 .center-box {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
      
    </style>
</head>
<body>
    <h3>div水平垂直居中的第二种方法</h3>
    <div class="demo-title">方法2: Grid</div>
    <div class="demo-container method2">
     
        <div class="center-box">Grid</div>
    </div>
</body>
</html>